<?php

namespace app\admin\controller\addfamily;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Request;

/**
 * 加入家庭
 *
 */
class Jiaru extends Backend
{
    protected $model = null;
    protected $noNeedRight = ['table1', 'table2'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\Home;
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit, $page, $alias, $bind, $tiaojian) = $this->buildparams();
            if ($tiaojian) {
                $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
            } else {
                $list = $this->model
                    ->where("number", 111)
                    ->order($sort, $order)
                    ->paginate($limit);
                $result = array("total" => $list->total(), "rows" => $list->items());
            }
            return json($result);
        }

        return $this->view->fetch();
    }
    public function jiaru($ids)
    {
        $row = $this->model->get(['id' => $ids])->toArray();
        $admin = $this->auth->getUserInfo();
        $access = array("group_id" => 3);
        $accesstui = array("group_id" => 4);
        $home_id = array( "home_id" => $row["id"]);
        $home_idtui = array( "home_id" => null);
        $home_size = array(
            "home_size" => $row["home_size"] + 1
        );
        $accessEdit = Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($access);
        if ($accessEdit) {
            $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_id);
            if ($adminEdit) {
                $homeEdit = $this->model->where("id", $row["id"])->update($home_size);
                if ($homeEdit) {
                    $this->auth->logout();
                    $this->success('加入成功', 'index/logout', '', 1, []);
                } else {
                    $adminEdit =  Db::table('jc_admin')->where("id", $admin['id'])->update($home_idtui);
                    $this->error('加入失败');
                }
            } else {
                $accessEdit = Db::table('jc_auth_group_access')->where("uid", $admin['id'])->update($accesstui);
                $this->error('加入失败');
            }
        } else {
            $this->error('加入失败');
        }
    }
}
